/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Tabela.java
 *
 * Created on 22.12.2011., 14.41.31
 */

package tok.predmeta;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author mega
 */
public class Tabela extends javax.swing.JFrame{
    static String Brpredmeta;


    public Tabela() {
        initComponents();
        


        jTable1.setVisible(false);
        Reminder r= new Reminder();
 



        Calendar cal1 = new GregorianCalendar();
        Calendar cal2 = new GregorianCalendar();
        Calendar cal22 = new GregorianCalendar();
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        String month = String.valueOf(gregorianCalendar.get(GregorianCalendar.MONTH));
        int Month = Integer.parseInt(month);
        String day = String.valueOf(gregorianCalendar.get(GregorianCalendar.DAY_OF_MONTH));

        int Day = Integer.parseInt(day);
        String year = String.valueOf(gregorianCalendar.get(GregorianCalendar.YEAR));
        int Year = Integer.parseInt(year);




        // Load the JDBC driver
        String driverName = "com.mysql.jdbc.Driver"; // MySQL  JDBC driver
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Pretraga.class.getName()).log(Level.SEVERE, null, ex);
        }

// Create a connection to the database
        String serverName = "192.168.88.10";
        String mydatabase = "tok_predmeta";
        String url = "jdbc:mysql://" + serverName + "/" + mydatabase + "?useUnicode=true&characterEncoding=utf8"; // a JDBC url
        String username = "tokpred";
        String password = "Lx3sAQdnYJFTxBhs";
        boolean start = false;

        try {
             java.sql.Connection connection = DriverManager.getConnection(url, username, password);
             java.sql.PreparedStatement s = connection.prepareStatement("SELECT *  FROM predmeti"); //LEFT JOIN state ON (predmeti.sifra=state.sifra) ");

            s.executeQuery();
            java.sql.PreparedStatement su = connection.prepareStatement("DELETE from predmeti2");
                    su.execute();

            ResultSet rs = s.getResultSet();


            int count = 0;
            while (rs.next()) {
                int idNum = rs.getInt("id");
                String sifra = rs.getString("sifra");
                  String korisnicka_sifra = rs.getString("korisnicka_sifra");
                String ime = rs.getString("ime");
                String prezime = rs.getString("prezime");
                String adresa = rs.getString("adresa");
                String jmbg = rs.getString("jmbg");
                String tip = rs.getString("tip");
                String datum = rs.getString("datum");
                //  String datum2 = rs.getString("date");
                String[] datum_predmeta = datum.split("/");



                int Dan = Integer.parseInt(datum_predmeta[0]);
                int Mesec = Integer.parseInt(datum_predmeta[1]) - 1;
                int Godina = Integer.parseInt(datum_predmeta[2]);
                int broj_dana2 = r.daysBetween(cal22.getTime(), cal1.getTime());
                broj_dana2 = Math.abs(broj_dana2);
                cal1.set(Year, Month, Day);
                cal2.set(Godina, Mesec, Dan);

                int broj_dana = r.daysBetween(cal2.getTime(), cal1.getTime());
                broj_dana = Math.abs(broj_dana);
                
              

                  if (broj_dana < 16 ) {


                    java.sql.PreparedStatement e = connection.prepareStatement("INSERT INTO predmeti2 (sifra,korisnicka_sifra,ime,prezime,adresa,jmbg,tip,datum,korisnik) VALUES "
                                    + "(?,?,?,?,?,?,?,?,?)");

                            e.setString(1, sifra);
                            e.setString(2, korisnicka_sifra);
                            e.setString(3, ime);
                            e.setString(4, prezime);
                            e.setString(5, adresa);
                            e.setString(6, jmbg);
                            e.setString(7, tip);
                            e.setString(8, datum);
                             e.setString(9, Login.user);
                            e.executeUpdate();
                }




                count++;


            }


            s.close();
            connection.close();

            jTable1.setVisible(true);
            jTable1.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent evt) {
                    int col = jTable1.getSelectedColumn();
                    int row = jTable1.getSelectedRow();
                    jTextField1.setText((String) jTable1.getModel().getValueAt(row, col));
                }
            });






           
        } catch (SQLException ex) {
            Logger.getLogger(Pretraga.class.getName()).log(Level.SEVERE, null, ex);

        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        tok_predmetaPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("tok_predmetaPU").createEntityManager();
        predmeti2Query = java.beans.Beans.isDesignTime() ? null : tok_predmetaPUEntityManager.createQuery("SELECT p FROM Predmeti2 p");
        predmeti2List = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : predmeti2Query.getResultList();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setBackground(new java.awt.Color(204, 204, 204));
        jTable1.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(0, 153, 204), null));
        jTable1.setCellSelectionEnabled(true);
        jTable1.setMinimumSize(new java.awt.Dimension(135, 0));

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, predmeti2List, jTable1);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${korisnik}"));
        columnBinding.setColumnName("Korisnik");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${datum}"));
        columnBinding.setColumnName("Datum");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${tip}"));
        columnBinding.setColumnName("Tip");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${jmbg}"));
        columnBinding.setColumnName("Jmbg");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${adresa}"));
        columnBinding.setColumnName("Adresa");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prezime}"));
        columnBinding.setColumnName("Prezime");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${ime}"));
        columnBinding.setColumnName("Ime");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${korisnickaSifra}"));
        columnBinding.setColumnName("Korisnicka Sifra");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${sifra}"));
        columnBinding.setColumnName("Sifra");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Назад");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Детаљније");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("Шифра предмета");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(31, 31, 31)
                            .addComponent(jButton2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton1))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 912, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addContainerGap(62, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-999)/2, (screenSize.height-566)/2, 999, 566);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Frame1 F = new Frame1();
        dispose();
        F.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        Brpredmeta = jTextField1.getText();

        Detail D = new Detail();

         dispose();
        
        D.setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Tabela().setVisible(true);

            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private java.util.List<tok.predmeta.Predmeti2> predmeti2List;
    private javax.persistence.Query predmeti2Query;
    private javax.persistence.EntityManager tok_predmetaPUEntityManager;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables


  



  
    

    


    

}
